package zhihui.mobile.ui.shop.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zgm.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.config.Config;


/**
 * Created by 张公铭 on 2017/9/28 0028.
 */

public class ShopListAdapter extends BaseAdapter
{
    private List<SpuInfo> mList = new ArrayList<>();
    private Context mContext;
    private IItemOnClickListener listener;

    public interface IItemOnClickListener
    {
        void goShopDetails(int shopId, int type);
    }

    public ShopListAdapter(Context context)
    {
        mContext = context;
    }

    public void setList(List<SpuInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    public void addList(List<SpuInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.addAll(list);
    }

    public void setListener(IItemOnClickListener listener)
    {
        this.listener = listener;
    }

    @Override
    public int getCount()
    {
        if (mList.size() % 2 > 0)
        {
            return mList.size() / 2 + 1;
        } else
        {
            return mList.size() / 2;
        }
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView iv_left = null;
        TextView tv_name = null;
        TextView tv_price = null;
        TextView tv_xl = null;
        LinearLayout ly_left = null;
        LinearLayout ly_right = null;
        ImageView iv_right = null;
        TextView tv_right_name = null;
        TextView tv_right_price = null;
        TextView tv_right_xl = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shop_item_utils, null);
            iv_left = (ImageView) convertView.findViewById(R.id.iv_recommend_left_img);
            tv_name = (TextView) convertView.findViewById(R.id.tv_recommend_left_name);
            tv_price = (TextView) convertView.findViewById(R.id.tv_recommend_left_price);
            tv_xl = (TextView) convertView.findViewById(R.id.tv_recommend_left_xl);
            ly_left = (LinearLayout) convertView.findViewById(R.id.ly_recommend_left);

            ly_right = (LinearLayout) convertView.findViewById(R.id.ly_recommend_right);
            iv_right = (ImageView) convertView.findViewById(R.id.iv_recommend_right_img);
            tv_right_name = (TextView) convertView.findViewById(R.id.tv_recommend_right_name);
            tv_right_price = (TextView) convertView.findViewById(R.id.tv_recommend_right_price);
            tv_right_xl = (TextView) convertView.findViewById(R.id.tv_recommend_right_xl);

            ViewCache viewCache = new ViewCache();
            viewCache.iv_left = iv_left;
            viewCache.tv_name = tv_name;
            viewCache.tv_price = tv_price;
            viewCache.tv_xl = tv_xl;
            viewCache.ly_left = ly_left;
            viewCache.ly_right = ly_right;
            viewCache.iv_right = iv_right;
            viewCache.tv_right_name = tv_right_name;
            viewCache.tv_right_price = tv_right_price;
            viewCache.tv_right_xl = tv_right_xl;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            iv_left = viewCache.iv_left;
            tv_name = viewCache.tv_name;
            tv_price = viewCache.tv_price;
            tv_xl = viewCache.tv_xl;
            ly_left = viewCache.ly_left;
            ly_right = viewCache.ly_right;
            iv_right = viewCache.iv_right;
            tv_right_name = viewCache.tv_right_name;
            tv_right_price = viewCache.tv_right_price;
            tv_right_xl = viewCache.tv_right_xl;
        }

        int distance = mList.size() - position * 2;
        int cellCount = distance >= 2 ? 2 : distance;
        final List<SpuInfo> itemList = mList.subList(position * 2, position * 2 + cellCount);
        if (itemList.size() > 0)
        {
            final SpuInfo lefSpu = itemList.get(0);
            ImageLoader.getInstance().displayImage(Config.FILE_URL + lefSpu.getSpuimg(), iv_left);
            tv_name.setText(lefSpu.getTitle());
            tv_price.setText("￥" + StringUtils.doubleFormat((double) lefSpu.getMinprice() / 100));
            tv_xl.setText("已售:" + lefSpu.getSales());
            ly_left.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    listener.goShopDetails(lefSpu.getId(), 0);
                }
            });

            if (itemList.size() > 1)
            {
                final SpuInfo rightSpu = itemList.get(1);
                ImageLoader.getInstance().displayImage(Config.FILE_URL + rightSpu.getSpuimg(), iv_right);
                tv_right_name.setText(rightSpu.getTitle());
                tv_right_price.setText("￥" + StringUtils.doubleFormat((double) rightSpu.getMinprice() / 100));
                tv_right_xl.setText("已售:" + rightSpu.getSales());
                ly_right.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.goShopDetails(rightSpu.getId(), 0);
                    }
                });
            } else
            {
                ly_right.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    class ViewCache
    {
        public ImageView iv_left = null;
        public TextView tv_name = null;
        public TextView tv_price = null;
        public TextView tv_xl = null;
        public LinearLayout ly_left = null;
        public LinearLayout ly_right = null;
        public ImageView iv_right = null;
        public TextView tv_right_name = null;
        public TextView tv_right_price = null;
        public TextView tv_right_xl = null;
    }
}
